<?php
/**
 * 活动宝藏
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 11:26
 */
namespace App;

use Model\ActivityTreasureMod;
use Pub\cache\Cache;

class ActivityTreasure extends Action
{
    private $ActivityTreasure = null;
	public function __construct()
	{
		parent::__construct();
		$this->ActivityTreasure = new ActivityTreasureMod();
	}

    /**
     * 新增活动的宝藏
     * @param data  新增的宝藏数据
     */
   public function add()
   {
       Cache::remove(Cache::ACTIVITY_INFO,array('activity_id'=>$this->_gpc['data']['activity_id']));
        if($this->ActivityTreasure->insert($this->_gpc['data'])){
            $this->_result = array('status'=>'success');
        }
        $this->result($this->_result);
   }

    /**
     * 编辑活动的宝藏
     * @param treasure_id 投放的宝藏id
     * @param data 修改的数据
     */
   public function edit()
   {
        Cache::remove(Cache::ACTIVITY_INFO,array('activity_id'=>$this->_gpc['data']['activity_id']));
        if($this->ActivityTreasure->update($this->_gpc['data'],array('id'=>$this->_gpc['treasure_id']))){
            $this->_result = array('status'=>'success');
        }
        $this->result($this->_result);
   }

    /**
     * 获取活动投放的宝藏列表
     * @param activity_id 活动id
     * @param type 宝藏类型
     */
   public function getList()
   {
        $_list = $this->ActivityTreasure->findAll(array('activity_id'=>$this->_gpc['activity_id'],'type'=>$this->_gpc['type']),array('order'=>array('status','asc','id','desc')),array('id','name','icon','money','number','user_number','give_number','win_odds','status'));
        if(count($_list) > 0){
            foreach($_list as $_k=>$_v){
                $_list[$_k]['icon_url'] = tomedia($_v['icon']);
                if($_v['user_number'] == 0) $_list[$_k]['user_number'] = '不限量';
            }
            $this->_result = array('status'=>'success','data'=>$_list);
        }
        $this->result($this->_result);
   }

    /**
     *取消投放宝藏，删除投放的宝藏
     * @param treasure_id  投放的宝藏id
     * @param status 状态
     */
   public function changeStatus()
   {
       if($this->ActivityTreasure->update(array('status'=>$this->_gpc['status']),array('id'=>$this->_gpc['treasure_id']))){
           Cache::remove(Cache::ACTIVITY_INFO,array('activity_id'=>$this->_gpc['activity_id']));
           $this->_result = array('status'=>'success');
       }
       $this->result($this->_result);
   }

}